package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IPermissionService permissionService;
    @RequestMapping("/login")
    @ResponseBody
    public JsonResult login(String username, String password) {
        try {
            //登入
            //UsernamePasswordToken token = new UsernamePasswordToken(username, password);
           // SecurityUtils.getSubject().login(token);
            return new JsonResult();
        } /*catch (UnknownAccountException e) {
            return new JsonResult(false,"账号不存在" );
        } catch (IncorrectCredentialsException e) {
            return new JsonResult(false,"密码错误" );
        }*/ catch (Exception e) {
            return new JsonResult(false,"登入异常,请联系管理员" );
        }
    }
    @RequestMapping("/logout")
    public String  logout() {
     // UserContext.getSession().invalidate();
      return "redirext:/login.html";
    }

    @RequestMapping("/updatePwd")
    @ResponseBody
    public JsonResult updatePwd(String oldPassword, String newPassword, HttpSession session) {
        try {
            Employee employee = employeeService.selectOldPwd(oldPassword);
            employeeService.updatePwd(employee.getId(), newPassword);
            session.invalidate();
            return new JsonResult(true, "yes");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, e.getMessage());
        }
    }
    @RequestMapping("/resetPwd")
    @ResponseBody
    public JsonResult resetPwd(String newPassword,Long id) {
        try {
            Employee employee = employeeService.select(id);
            employeeService.updatePwd(employee.getId(), newPassword);
            return new JsonResult(true, "yes");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, e.getMessage());
        }
    }

    @RequestMapping("/toResetPwd")
    public String toResetPwd(Model model, Long id) {
        Employee employee = employeeService.select(id);
        model.addAttribute(employee);
        return "employee/resetPwd";
    }




}
